﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PlatWL.Data;

namespace PlatWL.Services.Services
{
    public class FileManage
    {
        public string GetFileUrl(string fileIds, int width = 0, int height = 0)
        {
            try
            {
                string filename = string.Empty;
                if (!string.IsNullOrEmpty(fileIds) && fileIds.ToLower().Trim() != "null" && fileIds.ToLower().Trim() != "undefined")
                {
                    //获取第一个文件
                    int fileId = int.Parse(fileIds.Split(',')[0]);
                    var file = new pt_base_fileb().GetEntity(d => d.ID == fileId);
                    List<string> extendsVideo = new List<string>() { "avi", "mp4", "flv", "3gp", "wmv", "ra", "ram", "rmvb" };
                    List<string> extends = new List<string>() { "jpg", "jpeg", "png", "bmp", "webp", "gif", "tiff" };

                    //如果是视频
                    //if (extendsVideo.Contains(file.FileExtendName.ToLower()))
                    //{
                    //    //filename = string.Format("{0}/{1}-{2}", ConfigurationManager.AppSettings["videoServer"], file.FileMD5, ConfigurationManager.AppSettings["videoOut"]);
                    //    filename = string.Format("{0}/{1}", ConfigurationManager.AppSettings["videoServer"], file.FileMD5);
                    //}
                    //如果是图片
                     if (extends.Contains(file.FileExtendName.ToLower()) && width > 0 && height > 0)
                    {
                        filename = ConfigurationManager.AppSettings["fileServer"] + "/document/thumbnail/" + file.FileMD5 + "/" + width + "/" + height;
                        //filename = string.Format("{0}/{1}?x-oss-process=image/resize,m_fill,w_{2},h_{3}", ConfigurationManager.AppSettings["imgServer"], file.FileMD5, width, height);
                    }
                    else
                    {
                        filename = ConfigurationManager.AppSettings["fileServer"] + "/document/" + file.FileMD5;
                    }

                }
                return filename;
            }
            catch (Exception)
            {
                return "http://www.djusu.com/mobile/image/loading.png";
            }
        }
        public void AddFile(ref Msg_Result msg, JArray Files)
        {
            var lstFILES = new List<pt_base_file>();
            foreach (var item in Files)
            {
                int index = item["filename"].ToString().LastIndexOf('.');
                string filename = item["filename"].ToString().Substring(0, index);

                pt_base_file newfile = new pt_base_file();
                newfile.Name = filename;
                newfile.FileMD5 = item["md5"].ToString();
                newfile.FileSize = item["filesize"].ToString();
                newfile.FileVersin = 0;
                newfile.CRDate = DateTime.Now;
                newfile.CRUser = "";
                //newfile.UPDDate = date;
                newfile.UPUser = "";
                newfile.FileExtendName = item["filename"].ToString().Substring(index + 1);
                if (new List<string>() { "txt", "html", "doc", "jpg", "gif", "png", "bmp", "jpeg" }.Contains(newfile.FileExtendName.ToLower()))
                {
                    newfile.ISYL = "Y";
                }

                //视频文需要传输到阿里云转码
                //if (new List<string>() { "mp4", "wmv", "flv" }.Contains(newfile.FileExtendName.ToLower()))
                //{
                //    var oldModel = new PT_Base_File().GetEntities(w => w.FileMD5 == newfile.FileMD5 && w.ISYL == "Y").FirstOrDefault();
                //    if (oldModel != null)
                //    {
                //        newfile.ISYL = "Y";
                //        newfile.FileUrl = oldModel.FileUrl;
                //    }
                //    else
                //    {
                //        newfile.ISYL = "N";
                //    }
                //}
                new pt_base_fileb().Insert(newfile);
                lstFILES.Add(newfile);
            }

            msg.result = lstFILES;

        }
    }
}
